package com.example.controller;


import com.example.pojo.Book;
import com.example.result.Code;
import com.example.result.Result;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author luqi
 * @Date 2025 06 19 09 19
 **/

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book){
        if (!bookService.SaveNewBook(book)){
            return Result.error(Code.ERROR_SAVE,"添加失败了~~");
        }else {
            return Result.success(Code.SUCCESS_SAVE,"添加成功！");
        }
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        if (!bookService.DeleteBookById(id)){
            return Result.error(Code.ERROR_DELETE,"有股神秘的力量在阻止你删除~~");
        }else {
            return Result.success(Code.SUCCESS_DELETE,"删除成功！");
        }
    }
    @PutMapping
    public Result updateBook(@RequestBody Book book){
        if (!bookService.UpdateBookInformation(book)){
            return Result.error(Code.ERROR_UPDATE,"修改失败了~~");
        }else {
            return Result.success(Code.SUCCESS_UPDATE,"修改成功！");
        }
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Book data = bookService.SearchBookById(id);
        if (data == null){
            return Result.error(Code.ERROR_SEARCH,"数据不存在~~");
        }else {
            return Result.success(Code.SUCCESS_SEARCH,"查找到如下数据~~",data);
        }
    }
    @GetMapping
    public Result getAll(){
        List<Book> data = bookService.SearchAllBook();
        if (data == null){
            return Result.error(Code.ERROR_SEARCH_ALL,"没有数据~~");
        }else {
            return Result.success(Code.SUCCESS_SEARCH_ALL,"查找到如下数据~~",data);
        }
    }
}
